package com.qq.employee.controller;

import com.qq.employee.service.AttendanceService;
import com.qq.employee.utity.Attendance;
import com.qq.employee.utity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {
    @Autowired
    AttendanceService attendanceService;

    @RequestMapping("/list")
    public  String  listAttendance(Model model){
    List<Attendance> listAttendance = attendanceService.listAttendance();
    model.addAttribute("atts",listAttendance);
        return "att/list";
    }
    @RequestMapping("/insert")
        public String insertAttendance(Attendance attendance) {
             attendanceService.insertAttendance(attendance);
             return "redirect:/attendance/list";
        }
    @PostMapping("/att/{id}")
            public String deleteAttendance(@PathVariable("id") Integer id) {
                 attendanceService.deleteAttendance(id);
                 return "redirect:/attendance/list";
            }
        @RequestMapping("/update")
            public String updateAttendance(Attendance attendance) {
                 attendanceService.updateAttendance(attendance);
            return "redirect:/attendance/list";
            }
    @GetMapping("/add")
    public String toAddPage(){
    return "att/add";
}

    @GetMapping("/att/{id}")
    public String toUpdatePage(@PathVariable("id") Integer id, Model model){
        Attendance attendance = attendanceService.get(id);
        model.addAttribute("att",attendance);
        return "att/edit";
    }

}
